import io from 'socket.io-client';
import { config } from '../../config/app'

let socket = ''

export const listen = (action) => {

    if (socket === '') socket = io('localhost:' + config.port)

    socket.on('connect', () => {
        action({
            type: 'connect',
            state: 'ok',
            msg: '已连接服务器！'
        })
    })

    socket.on('disconnect', () => {
        action({
            type: 'msg',
            state: 'war',
            msg: '与接服务器链接已断开！'
        })
    })

    socket.on('list_all', (list) => {
        action({
            type: 'list',
            data: list
        })
    })


}

export const emit = (action) => {
    socket.emit(action.type, action.data)
}